package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.mapper.IndexItemMapper;
import com.rongke.model.Goods;
import com.rongke.model.IndexItem;
import com.rongke.model.PopGoods;
import com.rongke.service.GoodsService;
import com.rongke.service.IndexItemService;

import com.rongke.service.PopGoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.*;

/**
 * @IndexItemController
 * @主页栏目Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/indexItem")
@Transactional
@CrossOrigin
public class IndexItemController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private IndexItemService indexItemService;
    @Autowired
    private PopGoodsService popGoodsService;
    @Autowired
    private GoodsService goodsService;

    /**
     * @添加主页栏目
     * @param indexItem
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addIndexItem(@RequestBody IndexItem indexItem){
        log.debug("添加主页栏目");
        indexItemService.insert(indexItem);
        return JsonResp.ok(indexItem);
    }

    /**
     * @修改主页栏目
     * @param indexItem
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateIndexItem(@RequestBody IndexItem indexItem){
        log.debug("修改主页栏目");
        indexItemService.updateById(indexItem);
        return JsonResp.ok(indexItem);
    }
    /**
     * @根据id查找主页栏目
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectIndexItem(Long id){
        log.debug("查找主页栏目");
        IndexItem indexItem = indexItemService.selectById(id);
        return JsonResp.ok(indexItem);
    }

    /**
     * @根据条件查找主页栏目
     * @param indexItem
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectIndexItemByCondition(@RequestBody IndexItem indexItem){
        log.debug("根据条件查找主页栏目");
        if(indexItem==null)
            return JsonResp.toFail("参数不能为空");
        List<IndexItem> indexItemList=indexItemService.selectByCondition(indexItem);
        return JsonResp.ok(indexItemList);
    }

    /**
     * 根据栏目编号获取栏目基本信息
     * @param num
     * @return
     */
    @RequestMapping(value="/selectItemInfoByItemNum", method = RequestMethod.GET)
    public JsonResp selectItemInfoByItemNum(Integer num){
        log.debug("根据栏目编号获取栏目基本信息");
        if(null==num){
            return JsonResp.toFail("参数不能为空");
        }
        EntityWrapper<IndexItem> ew=new EntityWrapper<>();
        ew.eq("item_num",num);
        IndexItem indexItem=indexItemService.selectOne(ew);
        return JsonResp.ok(indexItem);
    }

    /**
     * 品牌馆等主页根据产品线获取栏目和主页商品
     * @param goodsType
     * @return
     */
    @RequestMapping(value = "/fetchHomepageItem",method = RequestMethod.GET)
    public JsonResp fetchHomepageItem(Integer goodsType){
        log.debug("品牌馆等主页根据产品线获取栏目和主页商品");
        EntityWrapper<IndexItem> ew=new EntityWrapper<>();
        ew.eq("goods_type",goodsType);
        List<IndexItem> indexItems=indexItemService.selectList(ew);
        if(!indexItems.isEmpty()){
            indexItems.forEach(e->{
                Integer type=e.getItemNum();//
                Integer indexNum=e.getIndexNum();//首页展示个数
                Page<PopGoods> page=new Page<>(1,indexNum);
                Map map=new HashMap();
                Integer startIndex=(page.getCurrent()-1)*page.getSize();
                map.put("startIndex",startIndex);
                map.put("size",page.getSize());
                map.put("type",type);
                List<PopGoods> popGoods = popGoodsService.selectPopGoodsList(map);
                List<Goods> goodsList=new ArrayList<>();
                if(!popGoods.isEmpty()){
                    popGoods.forEach(u->{
                        goodsService.fetchGoodsInventoryStatus(u.getGoods());//判断库存状态
                        goodsList.add(u.getGoods());
                    });
                }
                e.setGoodsList(goodsList);
            });
        }
        return JsonResp.ok(indexItems);
    }

    /**
     * 根据栏目编号获取栏目基本信息和商品列表
     * @param itemNum
     * @return
     */
    @RequestMapping(value = "/fetchHomePageItemByItemNum",method = RequestMethod.GET)
    public JsonResp fetchHomePageItemByItemNum(Integer itemNum,Page<PopGoods> page){
        log.debug("根据栏目编号获取栏目基本信息和商品列表");
        EntityWrapper<IndexItem> ew=new EntityWrapper<>();
        ew.eq("item_num",itemNum);
        IndexItem indexItem=indexItemService.selectOne(ew);
        if(null!=indexItem){
            Map map=new HashMap();
            Integer startIndex=(page.getCurrent()-1)*page.getSize();
            map.put("startIndex",startIndex);
            map.put("size",page.getSize());
            map.put("type",itemNum);
            List<PopGoods> popGoods = popGoodsService.selectPopGoodsList(map);
            List<Goods> goodsList=new ArrayList<>();
            if(!popGoods.isEmpty()){
                popGoods.forEach(u->{
                    goodsService.fetchGoodsInventoryStatus(u.getGoods());//判断库存状态
                    goodsList.add(u.getGoods());
                });
            }
            indexItem.setGoodsList(goodsList);
        }
        return JsonResp.ok(indexItem);
    }

    /**
     * 后台主页获取所有动态栏目item的名称
     * @return
     */
    @RequestMapping(value = "/getAllItemsAdmin",method = RequestMethod.GET)
    public JsonResp getAllItemsAdmin(){
        log.debug("后台主页获取所有动态栏目item的名称");
        EntityWrapper<IndexItem> ew=new EntityWrapper<>();
        List<IndexItem> items=indexItemService.selectList(ew);
        return JsonResp.ok(items);
    }
}
